home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ohlutil / tail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-24  |  22.9 KB  |  894 lines

  1. /* tail -- output last part of file(s)
  2.    Copyright (C) 1989, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /*
  19.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  20.  *
  21.  * To this port, the same copying conditions apply as to the
  22.  * original release.
  23.  *
  24.  * IMPORTANT:
  25.  * This file is not identical to the original GNU release!
  26.  * You should have received this code as patch to the official
  27.  * GNU release.
  28.  *
  29.  * MORE IMPORTANT:
  30.  * This port comes with ABSOLUTELY NO WARRANTY.
  31.  *
  32.  * $Header: e:/gnu/fileutil/RCS/tail.c'v 1.3.0.2 90/06/29 00:47:14 tho Stable $
  33.  */
  34.  
  35. /* Can display any amount of data, unlike the Unix version, which uses
  36.    a fixed size buffer and therefore can only deliver a limited number
  37.    of lines.
  38.  
  39.    Started by Paul Rubin <phr@ai.mit.edu>
  40.    Finished by David MacKenzie <djm@ai.mit.edu>
  41.  
  42.    Usage: tail [-n [+]#] [-lbcfqv] [+number [+]#] [+lines] [+blocks]
  43.           [+chars] [+follow] [+quiet] [+silent] [+verbose] [file...]
  44.  
  45.           tail [+/-#lbcfqv] [file...]
  46.  
  47.    Options:
  48.    -n, +number [+]#    Number of items to tail (default 10).
  49.             If the number starts with a `+', begin printing with
  50.             the #th item from the start of each file, instead of
  51.             from the end.
  52.    -l, +lines        Tail by lines (the default).
  53.    -b, +blocks        Tail by 512-byte blocks.
  54.    -c, +chars        Tail by characters.
  55.    -f, +follow        Loop forever trying to read more characters at the
  56.             end of the file, on the assumption that the file
  57.             is growing.  Ignored if reading from a pipe.
  58.             Cannot be used if more than one file is given.
  59.    -q, +quiet, +silent    Never print filename headers.
  60.    -v, +verbose        Always print filename headers.
  61.  
  62.    Reads from standard input if no files are given or when a filename of
  63.    ``-'' is encountered.
  64.    By default, filename headers are printed only more than one file
  65.    is given.  */
  66.  
  67. #include <stdio.h>
  68. #include <ctype.h>
  69. #include <sys/types.h>
  70. #include "getopt.h"
  71. #include "system.h"
  72.  
  73. #ifdef STDC_HEADERS
  74. #include <errno.h>
  75. #include <stdlib.h>
  76. #define ISDIGIT(c) (isdigit ((unsigned char) (c)))
  77. #else
  78. #define ISDIGIT(c) (isascii (c) && isdigit (c))
  79. char *malloc ();
  80. void free ();
  81.  
  82. extern int errno;
  83. #endif
  84.  
  85. #ifndef _POSIX_SOURCE
  86. long lseek();
  87. #endif
  88.  
  89. /* Number of items to tail. */
  90. #define DEFAULT_NUMBER 10
  91.  
  92. /* The number of bytes in a block (-b option). */
  93. #define BLOCKSIZE 512
  94.  
  95. /* Size of atomic reads. */
  96. #define BUFSIZE (BLOCKSIZE * 8)
  97.  
  98. /* Masks for the operation mode.  If neither CHARS nor BLOCKS is set,
  99.    tail operates by lines. */
  100. #define CHARS 1            /* Tail by characters. */
  101. #define BLOCKS 2        /* Tail by blocks. */
  102. #define FOREVER 4        /* Read from end of file forever. */
  103. #define START 8            /* Count from start of file instead of end. */
  104. #define HEADERS 16        /* Print filename headers. */
  105.  
  106. /* When to print the filename banners. */
  107. enum header_mode
  108. {
  109.   multiple_files, always, never
  110. };
  111.  
  112. #ifdef MSDOS
  113.  
  114. #include <io.h>
  115. #include <stdlib.h>
  116. #include <string.h>
  117.  
  118. extern  void main (int, char **);
  119. extern  void write_header (char *);
  120. extern  int tail (char *, int, int, long);
  121. extern  int tail_file (char *, int, long);
  122. extern  int tail_chars (char *, int, int, long);
  123. extern  int tail_lines (char *, int, int, long);
  124. extern  int file_lines (char *, int, long, long);
  125. extern  int pipe_lines (char *, int, long);
  126. extern  int pipe_chars (char *, int, long);
  127. extern  int start_chars (char *, int, long);
  128. extern  int start_lines (char *, int, long);
  129. extern  void dump_remainder (char *, int, int);
  130. extern  void xwrite (int, char *, int);
  131. extern  char *xmalloc (int);
  132. extern  long atou (char *);
  133. extern  char *basename (char *);
  134. extern  void error (int status, int errnum, char *message, ...);
  135. extern  void usage (void);
  136.  
  137. #else  /* not MSDOS */
  138.  
  139. char *xmalloc ();
  140. int file_lines ();
  141. int pipe_chars ();
  142. int pipe_lines ();
  143. int start_chars ();
  144. int start_lines ();
  145. int tail ();
  146. int tail_chars ();
  147. int tail_file ();
  148. int tail_lines ();
  149. long atou();
  150. void dump_remainder ();
  151. void error ();
  152. void usage ();
  153. void write_header ();
  154. void xwrite ();
  155.  
  156. extern int errno;
  157.  
  158. #endif /* not MSDOS */
  159.  
  160. /* The name this program was run with. */
  161. char *program_name;
  162.  
  163. struct option long_options[] =
  164. {
  165.   {"number", 1, NULL, 'n'},
  166.   {"lines", 0, NULL, 'l'},
  167.   {"blocks", 0, NULL, 'b'},
  168.   {"chars", 0, NULL, 'c'},
  169.   {"follow", 0, NULL, 'f'},
  170.   {"quiet", 0, NULL, 'q'},
  171.   {"silent", 0, NULL, 'q'},
  172.   {"verbose", 0, NULL, 'v'},
  173.   {NULL, 0, NULL, 0}
  174. };
  175.  
  176. void
  177. main (argc, argv)
  178.      int argc;
  179.      char **argv;
  180. {
  181.   enum header_mode header_mode = multiple_files;
  182.   int errors = 0;        /* Exit status. */
  183.   int mode = 0;            /* Flags. */
  184.   /* In START mode, the number of items to skip before printing; otherwise,
  185.      the number of items at the end of the file to print.  Initially, -1
  186.      means the value has not been set. */
  187.   long number = -1;
  188.   int c;            /* Option character. */
  189.   int longind;            /* Index in `long_options' of option found. */
  190.  
  191.   program_name = argv[0];
  192. #ifdef MSDOS
  193.   strlwr (program_name);
  194. #endif /* not MSDOS */
  195.  
  196.   if (argc > 1
  197.       && ((argv[1][0] == '-' && ISDIGIT (argv[1][1]))
  198.       || (argv[1][0] == '+' && (ISDIGIT (argv[1][1]) || argv[1][1] == 0))))
  199.     {
  200.       /* Old option syntax: a dash or plus, one or more digits, and one or
  201.      more option letters. */
  202.       if (argv[1][0] == '+')
  203.     mode |= START;
  204.       if (ISDIGIT (argv[1][1]))
  205.     {
  206.       for (number = 0, ++argv[1]; ISDIGIT (*argv[1]); ++argv[1])
  207.         number = number * 10 + *argv[1] - '0';
  208.       /* Parse any appended option letters with getopt. */
  209.       if (*argv[1])
  210.         *--argv[1] = '-';
  211.       else
  212.         argv[1] = "-l";
  213.     }
  214.       else
  215.     argv[1] = "-l";
  216.     }
  217.   while ((c = getopt_long (argc, argv, "n:lbcfqv", long_options, &longind))
  218.      != EOF)
  219.     {
  220.       if (c == 0)
  221.     c = long_options[longind].val;
  222.       switch (c)
  223.     {
  224.     case 'n':
  225.       if (*optarg == '+')
  226.         {
  227.           mode |= START;
  228.           ++optarg;
  229.         }
  230.       number = atou (optarg);
  231.       if (number == -1)
  232.         error (1, 0, "invalid number `%s'", optarg);
  233.       break;
  234.     case 'l':
  235.       mode &= ~(CHARS | BLOCKS);
  236.       break;
  237.     case 'b':
  238.       mode |= BLOCKS;
  239.       mode &= ~CHARS;
  240.       break;
  241.     case 'c':
  242.       mode |= CHARS;
  243.       mode &= ~BLOCKS;
  244.       break;
  245.     case 'f':
  246. #ifndef MSDOS
  247.         mode |= FOREVER;
  248. #endif /* not MSDOS */
  249.       break;
  250.     case 'q':
  251.       header_mode = never;
  252.       break;
  253.     case 'v':
  254.       header_mode = always;
  255.       break;
  256.     default:
  257.       usage ();
  258.     }
  259.     }
  260.  
  261.   if (number == -1)
  262.     number = DEFAULT_NUMBER;
  263.  
  264.   /* To start printing with item `number' from the start of the file, skip
  265.      `number' - 1 items.  `tail +0' is actually meaningless, but for Unix
  266.      compatibility it's treated the same as `tail +1'. */
  267.   if (mode & START)
  268.     {
  269.       if (number)
  270.     --number;
  271.     }
  272.  
  273.   if (mode & BLOCKS)
  274.     number *= BLOCKSIZE;
  275.  
  276.   if (optind < argc - 1 && (mode & FOREVER))
  277.     error (1, 0, "cannot follow the ends of multiple files");
  278.  
  279.   if (header_mode == always
  280.       || header_mode == multiple_files && optind < argc - 1)
  281.     mode |= HEADERS;
  282.  
  283.   if (optind == argc)
  284.     errors |= tail_file ("-", mode, number);
  285.  
  286.   for (; optind < argc; ++optind)
  287.     errors |= tail_file (argv[optind], mode, number);
  288.  
  289.   exit (errors);
  290. }
  291.  
  292. /* Display the last `number' units of file `filename', controlled by
  293.    the flags in `mode'.  "-" for `filename' means the standard input.
  294.    Return 0 if successful, 1 if an error occurred. */
  295.  
  296. int
  297. tail_file (filename, mode, number)
  298.      char *filename;
  299.      int mode;
  300.      long number;
  301. {
  302.   int fd;
  303.  
  304. #ifdef MSDOS
  305.   strlwr (filename);
  306. #endif /* MSDOS */
  307.  
  308.   if (!strcmp (filename, "-"))
  309.     {
  310.       filename = "standard input";
  311.       if (mode & HEADERS)
  312.     write_header (filename);
  313.       return tail (filename, 0, mode, number);
  314.     }
  315.   else
  316.     {
  317.       fd = open (filename, O_RDONLY);
  318.       if (fd == -1)
  319.     {
  320.       error (0, errno, "%s", filename);
  321.       return 1;
  322.     }
  323.       else
  324.     {
  325.       int errors;
  326.  
  327.       if (mode & HEADERS)
  328.         write_header (filename);
  329.       errors = tail (filename, fd, mode, number);
  330.       close (fd);
  331.       return errors;
  332.     }
  333.     }
  334. }
  335.  
  336. void
  337. write_header (filename)
  338.      char *filename;
  339. {
  340.   static int first_file = 1;
  341.  
  342.   if (first_file)
  343.     {
  344.       xwrite (1, "==> ", 4);
  345.       first_file = 0;
  346.     }
  347.   else
  348.     xwrite (1, "\n==> ", 5);
  349.   xwrite (1, filename, strlen (filename));
  350.   xwrite (1, " <==\n", 5);
  351. }
  352.  
  353. /* Display the last `number' units of file `filename', open for reading
  354.    in `fd', controlled by `mode'.
  355.    Return 0 if successful, 1 if an error occurred. */
  356.  
  357. int
  358. tail (filename, fd, mode, number)
  359.      char *filename;
  360.      int fd;
  361.      int mode;
  362.      long number;
  363. {
  364.   if (mode & (CHARS | BLOCKS))
  365.     return tail_chars (filename, fd, mode, number);
  366.   else
  367.     return tail_lines (filename, fd, mode, number);
  368. }
  369.  
  370. /* Display the last part of file `filename', open for reading in`fd',
  371.    using `number' characters, controlled by `mode'.
  372.    Return 0 if successful, 1 if an error occurred. */
  373.  
  374. int
  375. tail_chars (filename, fd, mode, number)
  376.      char *filename;
  377.      int fd;
  378.      int mode;
  379.      long number;
  380. {
  381.   long length;
  382.  
  383.   if (mode & START)
  384.     {
  385.       if (lseek (fd, number, L_SET) < 0)
  386.     {
  387.       /* Reading from a pipe. */
  388.       mode &= ~FOREVER;
  389.       if (start_chars (filename, fd, number))
  390.         return 1;
  391.     }
  392.       dump_remainder (filename, fd, mode);
  393.     }
  394.   else
  395.     {
  396.       length = lseek (fd, 0L, L_XTND);
  397.       if (length >= 0)
  398.     {
  399.       if (length <= number)
  400.         /* The file is shorter than we want, or just the right size, so
  401.            print the whole file. */
  402.         lseek (fd, 0L, L_SET);
  403.       else
  404.         /* The file is longer than we want, so go back. */
  405.         lseek (fd, -number, L_XTND);
  406.       dump_remainder (filename, fd, mode);
  407.     }
  408.       else
  409.     return pipe_chars (filename, fd, number);
  410.     }
  411.   return 0;
  412. }
  413.  
  414. /* Display the last part of file `filename', open for reading on `fd',
  415.    using `number' lines, controlled by `mode'.
  416.    Return 0 if successful, 1 if an error occurred. */
  417.  
  418. int
  419. tail_lines (filename, fd, mode, number)
  420.      char *filename;
  421.      int fd;
  422.      int mode;
  423.      long number;
  424. {
  425.   long length;
  426.  
  427.   if (mode & START)
  428.     {
  429.       if (lseek (fd, 0L, L_SET) < 0)
  430.     mode &= ~FOREVER;
  431.       if (start_lines (filename, fd, number))
  432.     return 1;
  433.       dump_remainder (filename, fd, mode);
  434.     }
  435.   else
  436.     {
  437.       length = lseek (fd, 0L, L_XTND);
  438.       if (length >= 0)
  439.     {
  440.       if (length != 0 && file_lines (filename, fd, number, length))
  441.         return 1;
  442.       dump_remainder (filename, fd, mode);
  443.     }
  444.       else
  445.     return pipe_lines (filename, fd, number);
  446.     }
  447.   return 0;
  448. }
  449.  
  450. /* Print the last `number' lines from the end of file `fd'.
  451.    Go backward through the file, reading `BUFSIZE' bytes at a time (except
  452.    probably the first), until we hit the start of the file or have
  453.    read `number' newlines.
  454.    `pos' starts out as the length of the file (the offset of the last
  455.    byte of the file + 1).
  456.    Return 0 if successful, 1 if an error occurred. */
  457.  
  458. int
  459. file_lines (filename, fd, number, pos)
  460.      char *filename;
  461.      int fd;
  462.      long number;
  463.      long pos;
  464. {
  465.   char buffer[BUFSIZE];
  466.   int chars_read;
  467.   int i;            /* Index into `buffer' for scanning. */
  468.  
  469.   if (number == 0)
  470.     return 0;
  471.  
  472.   /* Set `chars_read' to the size of the last, probably partial, buffer;
  473.      0 < `chars_read' <= `BUFSIZE'. */
  474. #ifdef MSDOS                /* shut up the compiler */
  475.   chars_read = (int) (pos % (long) BUFSIZE);
  476. #else
  477.   chars_read = pos % BUFSIZE;
  478. #endif /* MSDOS */
  479.   if (chars_read == 0)
  480.     chars_read = BUFSIZE;
  481.   /* Make `pos' a multiple of `BUFSIZE' (0 if the file is short), so that all
  482.      reads will be on block boundaries, which might increase efficiency. */
  483.   pos -= chars_read;
  484.   lseek (fd, pos, L_SET);
  485.   chars_read = read (fd, buffer, chars_read);
  486.   if (chars_read == -1)
  487.     {
  488.       error (0, errno, "%s", filename);
  489.       return 1;
  490.     }
  491.  
  492.   /* Count the incomplete line on files that don't end with a newline. */
  493.   if (chars_read && buffer[chars_read - 1] != '\n')
  494.     --number;
  495.  
  496.   do
  497.     {
  498.       /* Scan backward, counting the newlines in this bufferfull. */
  499.       for (i = chars_read - 1; i >= 0; i--)
  500.     {
  501.       /* Have we counted the requested number of newlines yet? */
  502.       if (buffer[i] == '\n' && number-- == 0)
  503.         {
  504.           /* If this newline wasn't the last character in the buffer,
  505.              print the text after it. */
  506.           if (i != chars_read - 1)
  507.         xwrite (1, &buffer[i + 1], chars_read - (i + 1));
  508.           return 0;
  509.         }
  510.     }
  511.       /* Not enough newlines in that bufferfull. */
  512.       if (pos == 0)
  513.     {
  514.       /* Not enough lines in the file; print the entire file. */
  515.       lseek (fd, 0L, L_SET);
  516.       return 0;
  517.     }
  518.       pos -= BUFSIZE;
  519.       lseek (fd, pos, L_SET);
  520.     }
  521.   while ((chars_read = read (fd, buffer, BUFSIZE)) > 0);
  522.   if (chars_read == -1)
  523.     {
  524.       error (0, errno, "%s", filename);
  525.       return 1;
  526.     }
  527.   return 0;
  528. }
  529.  
  530. /* Print the last `number' lines from the end of the standard input,
  531.    open for reading as pipe `fd'.
  532.    Buffer the text as a linked list of LBUFFERs, adding them as needed.
  533.    Return 0 if successful, 1 if an error occured. */
  534.  
  535. int
  536. pipe_lines (filename, fd, number)
  537.      char *filename;
  538.      int fd;
  539.      long number;
  540. {
  541.   struct linebuffer
  542.   {
  543.     int nchars, nlines;
  544.     char buffer[BUFSIZE];
  545.     struct linebuffer *next;
  546.   };
  547.   typedef struct linebuffer LBUFFER;
  548.   LBUFFER *first, *last, *tmp;
  549.   int i;            /* Index into buffers. */
  550.   long total_lines = 0;        /* Total number of newlines in all buffers. */
  551.   int errors = 0;
  552.  
  553.   first = last = (LBUFFER *) xmalloc (sizeof (LBUFFER));
  554.   first->nchars = first->nlines = 0;
  555.   tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
  556.  
  557.   /* Input is always read into a fresh buffer. */
  558.   while ((tmp->nchars = read (fd, tmp->buffer, BUFSIZE)) > 0)
  559.     {
  560.       tmp->nlines = 0;
  561.       tmp->next = NULL;
  562.  
  563.       /* Count the number of newlines just read. */
  564.       for (i = 0; i < tmp->nchars; i++)
  565.     if (tmp->buffer[i] == '\n')
  566.       ++tmp->nlines;
  567.       total_lines += tmp->nlines;
  568.  
  569.       /* If there is enough room in the last buffer read, just append the new
  570.          one to it.  This is because when reading from a pipe, `nchars' can
  571.          often be very small. */
  572.       if (tmp->nchars + last->nchars < BUFSIZE)
  573.     {
  574.       bcopy (tmp->buffer, &last->buffer[last->nchars], tmp->nchars);
  575.       last->nchars += tmp->nchars;
  576.       last->nlines += tmp->nlines;
  577.     }
  578.       else
  579.     {
  580.       /* If there's not enough room, link the new buffer onto the end of
  581.          the list, then either free up the oldest buffer for the next
  582.          read if that would leave enough lines, or else malloc a new one.
  583.          Some compaction mechanism is possible but probably not
  584.          worthwhile. */
  585.       last = last->next = tmp;
  586.       if (total_lines - first->nlines > number)
  587.         {
  588.           tmp = first;
  589.           total_lines -= first->nlines;
  590.           first = first->next;
  591.         }
  592.       else
  593.         tmp = (LBUFFER *) xmalloc (sizeof (LBUFFER));
  594.     }
  595.     }
  596.   if (tmp->nchars == -1)
  597.     {
  598.       error (0, errno, "%s", filename);
  599.       errors = 1;
  600.       free ((char *) tmp);
  601.       goto free_lbuffers;
  602.     }
  603.  
  604.   free ((char *) tmp);
  605.  
  606.   /* This prevents a core dump when the pipe contains no newlines. */
  607.   if (number == 0)
  608.     goto free_lbuffers;
  609.  
  610.   /* Count the incomplete line on files that don't end with a newline. */
  611.   if (last->buffer[last->nchars - 1] != '\n')
  612.     {
  613.       ++last->nlines;
  614.       ++total_lines;
  615.     }
  616.  
  617.   /* Run through the list, printing lines.  First, skip over unneeded
  618.      buffers. */
  619.   for (tmp = first; total_lines - tmp->nlines > number; tmp = tmp->next)
  620.     total_lines -= tmp->nlines;
  621.  
  622.   /* Find the correct beginning, then print the rest of the file. */
  623.   if (total_lines > number)
  624.     {
  625.       char *cp;
  626.  
  627.       /* Skip `total_lines' - `number' newlines.  We made sure that
  628.          `total_lines' - `number' <= `tmp->nlines'. */
  629.       cp = tmp->buffer;
  630. #ifdef MSDOS                /* shut up the compiler */
  631.       for (i = (int) (total_lines - number); i; --i)
  632. #else /* not MSDOS */
  633.       for (i = total_lines - number; i; --i)
  634. #endif /* not MSDOS */
  635.     while (*cp++ != '\n')
  636.       /* Do nothing. */ ;
  637.       i = cp - tmp->buffer;
  638.     }
  639.   else
  640.     i = 0;
  641.   xwrite (1, &tmp->buffer[i], tmp->nchars - i);
  642.  
  643.   for (tmp = tmp->next; tmp; tmp = tmp->next)
  644.     xwrite (1, tmp->buffer, tmp->nchars);
  645.  
  646. free_lbuffers:
  647.   while (first)
  648.     {
  649.       tmp = first->next;
  650.       free ((char *) first);
  651.       first = tmp;
  652.     }
  653.   return errors;
  654. }
  655.  
  656. /* Print the last `number' characters from the end of pipe `fd'.
  657.    This is a stripped down version of pipe_lines.
  658.    Return 0 if successful, 1 if an error occurred. */
  659.  
  660. int
  661. pipe_chars (filename, fd, number)
  662.      char *filename;
  663.      int fd;
  664.      long number;
  665. {
  666.   struct charbuffer
  667.   {
  668.     int nchars;
  669.     char buffer[BUFSIZE];
  670.     struct charbuffer *next;
  671.   };
  672.   typedef struct charbuffer CBUFFER;
  673.   CBUFFER *first, *last, *tmp;
  674.   int i;            /* Index into buffers. */
  675.   long total_chars = 0;        /* Total characters in all buffers. */
  676.   int errors = 0;
  677.  
  678.   first = last = (CBUFFER *) xmalloc (sizeof (CBUFFER));
  679.   first->nchars = 0;
  680.   tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
  681.  
  682.   /* Input is always read into a fresh buffer. */
  683.   while ((tmp->nchars = read (fd, tmp->buffer, BUFSIZE)) > 0)
  684.     {
  685.       tmp->next = NULL;
  686.  
  687.       total_chars += tmp->nchars;
  688.       /* If there is enough room in the last buffer read, just append the new
  689.          one to it.  This is because when reading from a pipe, `nchars' can
  690.          often be very small. */
  691.       if (tmp->nchars + last->nchars < BUFSIZE)
  692.     {
  693.       bcopy (tmp->buffer, &last->buffer[last->nchars], tmp->nchars);
  694.       last->nchars += tmp->nchars;
  695.     }
  696.       else
  697.     {
  698.       /* If there's not enough room, link the new buffer onto the end of
  699.          the list, then either free up the oldest buffer for the next
  700.          read if that would leave enough characters, or else malloc a new
  701.          one.  Some compaction mechanism is possible but probably not
  702.          worthwhile. */
  703.       last = last->next = tmp;
  704.       if (total_chars - first->nchars > number)
  705.         {
  706.           tmp = first;
  707.           total_chars -= first->nchars;
  708.           first = first->next;
  709.         }
  710.       else
  711.         {
  712.           tmp = (CBUFFER *) xmalloc (sizeof (CBUFFER));
  713.         }
  714.     }
  715.     }
  716.   if (tmp->nchars == -1)
  717.     {
  718.       error (0, errno, "%s", filename);
  719.       errors = 1;
  720.       free ((char *) tmp);
  721.       goto free_cbuffers;
  722.     }
  723.  
  724.   free ((char *) tmp);
  725.  
  726.   /* Run through the list, printing characters.  First, skip over unneeded
  727.      buffers. */
  728.   for (tmp = first; total_chars - tmp->nchars > number; tmp = tmp->next)
  729.     total_chars -= tmp->nchars;
  730.  
  731.   /* Find the correct beginning, then print the rest of the file.
  732.      We made sure that `total_chars' - `number' <= `tmp->nchars'. */
  733.   if (total_chars > number)
  734. #ifdef MSDOS                /* shut up the compiler */
  735.     i = (int) (total_chars - number);
  736. #else /* not MSDOS */
  737.     i = total_chars - number;
  738. #endif /* not MSDOS */
  739.   else
  740.     i = 0;
  741.   xwrite (1, &tmp->buffer[i], tmp->nchars - i);
  742.  
  743.   for (tmp = tmp->next; tmp; tmp = tmp->next)
  744.     xwrite (1, tmp->buffer, tmp->nchars);
  745.  
  746. free_cbuffers:
  747.   while (first)
  748.     {
  749.       tmp = first->next;
  750.       free ((char *) first);
  751.       first = tmp;
  752.     }
  753.   return errors;
  754. }
  755.  
  756. /* Skip `number' characters from the start of pipe `fd', and print
  757.    any extra characters that were read beyond that.
  758.    Return 1 on error, 0 if ok.  */
  759.  
  760. int
  761. start_chars (filename, fd, number)
  762.      char *filename;
  763.      int fd;
  764.      long number;
  765. {
  766.   char buffer[BUFSIZE];
  767.   int chars_read = 0;
  768.  
  769.   while (number > 0 && (chars_read = read (fd, buffer, BUFSIZE)) > 0)
  770.     number -= chars_read;
  771.   if (chars_read == -1)
  772.     {
  773.       error (0, errno, "%s", filename);
  774.       return 1;
  775.     }
  776.   else if (number < 0)
  777. #ifdef MSDOS                /* |number| < 64k ??? */
  778.     xwrite (1, &buffer[chars_read + number], (unsigned int) (-number));
  779. #else /* not MSDOS */
  780.     xwrite (1, &buffer[chars_read + number], -number);
  781. #endif /* not MSDOS */
  782.   return 0;
  783. }
  784.  
  785. /* Skip `number' lines at the start of file or pipe `fd', and print
  786.    any extra characters that were read beyond that.
  787.    Return 1 on error, 0 if ok.  */
  788.  
  789. int
  790. start_lines (filename, fd, number)
  791.      char *filename;
  792.      int fd;
  793.      long number;
  794. {
  795.   char buffer[BUFSIZE];
  796.   int chars_read = 0;
  797.   int chars_to_skip = 0;
  798.  
  799.   while (number && (chars_read = read (fd, buffer, BUFSIZE)) > 0)
  800.     {
  801.       chars_to_skip = 0;
  802.       while (chars_to_skip < chars_read)
  803.     if (buffer[chars_to_skip++] == '\n' && --number == 0)
  804.       break;
  805.     }
  806.   if (chars_read == -1)
  807.     {
  808.       error (0, errno, "%s", filename);
  809.       return 1;
  810.     }
  811.   else if (chars_to_skip < chars_read)
  812.     xwrite (1, &buffer[chars_to_skip], chars_read - chars_to_skip);
  813.   return 0;
  814. }
  815.  
  816. /* Display file `filename' from the current position in `fd'
  817.    to the end.  If selected in `mode', keep reading from the
  818.    end of the file until killed. */
  819.  
  820. void
  821. dump_remainder (filename, fd, mode)
  822.      char *filename;
  823.      int fd;
  824.      int mode;
  825. {
  826.   char buffer[BUFSIZE];
  827.   int chars_read;
  828.  
  829. output:
  830.   while ((chars_read = read (fd, buffer, BUFSIZE)) > 0)
  831.     xwrite (1, buffer, chars_read);
  832.   if (chars_read == -1)
  833.     error (1, errno, "%s", filename);
  834. #ifndef MSDOS
  835.   if (mode & FOREVER)
  836.     {
  837.       sleep (1);
  838.       goto output;
  839.     }
  840. #endif /* not MSDOS */
  841. }
  842.  
  843. /* Write plus error check. */
  844.  
  845. void
  846. xwrite (fd, buffer, count)
  847.      int fd;
  848.      int count;
  849.      char *buffer;
  850. {
  851.   fd = write (fd, buffer, count);
  852.   if (fd != count)
  853.     error (1, errno, "write error");
  854. }
  855.  
  856. /* Allocate `size' bytes of memory dynamically, with error check. */
  857.  
  858. char *
  859. xmalloc (size)
  860.      int size;
  861. {
  862.   char *p;
  863.  
  864.   p = malloc ((unsigned) size);
  865.   if (p == NULL)
  866.     error (1, 0, "virtual memory exhausted");
  867.   return p;
  868. }
  869.  
  870. /* Convert `str', a string of ASCII digits, into an unsigned integer.
  871.    Return -1 if `str' does not represent a valid unsigned integer. */
  872.  
  873. long
  874. atou (str)
  875.      char *str;
  876. {
  877.   unsigned long value;
  878.  
  879.   for (value = 0; ISDIGIT (*str); ++str)
  880.     value = value * 10 + *str - '0';
  881.   return *str ? -1L : value;
  882. }
  883.  
  884. void
  885. usage ()
  886. {
  887.   fprintf (stderr, "\
  888. Usage: %s [-n [+]#] [-lbcfqv] [+number [+]#] [+lines] [+blocks]\n\
  889.        [+chars] [+follow] [+quiet] [+silent] [+verbose] [file...]\n\
  890. \n\
  891.        %s [+/-#lbcfqv] [file...]\n", program_name, program_name);
  892.   exit (1);
  893. }
  894.